package com.wolfking.converter.transform.extend;


import com.wolfking.converter.transform.AbstractExtendTransform;
import com.wolfking.converter.util.CaesarUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Slf4j
public class StringMaskTransform extends AbstractExtendTransform<String> {


    private final String format;

    public StringMaskTransform(){
        this("name");
    }

    public StringMaskTransform(String format) {
        this.format = format;
    }


    @Override
    public String transform(Object value) {
        if (Objects.isNull(value)) {
            return "";
        }
        String beforeValue;
        if (value instanceof String) {
            beforeValue = value.toString();
        } else if (value instanceof byte[]) {
            beforeValue = new String((byte[]) value, StandardCharsets.UTF_8);
        } else {
            beforeValue = value.toString();
        }

        if (CaesarUtil.isEncrypted(beforeValue)) {
            beforeValue = CaesarUtil.decode(beforeValue);
        }
        if ("name".equals(format)) {
            return maskName(beforeValue);
        } else if ("id_card".equalsIgnoreCase(format)) {
            return maskIdCard(beforeValue);
        } else if ("mobile".equalsIgnoreCase(format)) {
            return maskMobile(beforeValue);
        } else if ("bank_card".equalsIgnoreCase(format)) {
            return maskBankCard(beforeValue);
        } else {
            return maskName(beforeValue);
        }
    }


    private static String maskName(final String initName) {
        if (StringUtils.isBlank(initName)) {
            return initName;
        }

        int length = initName.trim().length();

        String last = StringUtils.substring(initName.trim(), length - 1, length);
        String prefix = StringUtils.substring(initName.trim(), 0,1);

        if (length == 2) {
            return "*" + last;
        } else if (length == 3) {
            return "*" + StringUtils.substring(initName.trim(), length - 2, length);
        } else if (length > 3) {
            return prefix + "**" + last;
        }
        return initName;
    }

    // 身份证号脱敏
    private static String maskIdCard(final String initIdCard) {
        if (StringUtils.isBlank(initIdCard)) {
            return initIdCard;
        }

        int len = initIdCard.length();

        if (len <= 10) {
            return initIdCard;
        }
        return initIdCard.substring(0, 6) + StringUtils.repeat("*", len - 10) + initIdCard.substring(len - 4);
    }

    // 手机号脱敏
    private static String maskMobile(final String initMobile) {
        if (StringUtils.isBlank(initMobile)) {
            return initMobile;
        }


        if (initMobile.length() != 11) {
            return initMobile;
        }
        return initMobile.trim().substring(0, 3) + "****" + initMobile.trim().substring(7);
    }

    // 银行卡号脱敏
    private static String maskBankCard(final String initBankCard) {
        if (StringUtils.isBlank(initBankCard)) {
            return initBankCard;
        }

        int len = initBankCard.length();

        if (len <= 8) {
            return initBankCard;
        }
        return initBankCard.substring(0, 4) + StringUtils.repeat("*", len - 8) + initBankCard.substring(len - 4);
    }
}
